﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Net.Sockets;

namespace University.Workshop.Model
{
    
    public class BannedUsers
    {
        public static BannedUsers Instance { get { if (instance == null)instance = new BannedUsers(); return instance; } }
        private static BannedUsers instance;
        public Collection<string> Users { get; private set; }
        public Collection<NetworkStream> Directions { get; private set; }
        public static object Mutex { get; set; }
        private BannedUsers()
        {
            Mutex = new object();
            Users = new Collection<string>();
            Directions = new Collection<NetworkStream>();
        }
        public void AddUser(string name, NetworkStream stream)
        {
            lock (Mutex)
            {
                Users.Add(name);
                Directions.Add(stream);
            }
        }
        public NetworkStream GetStream(string name)
        {
            lock (Mutex)
            {
                int index = Users.IndexOf(name);
                if (index < 0)
                    return null;
                return Directions[index];
            }
        }
        public void RetireUser(string name)
        {
            lock (Mutex)
            {
                int index = Users.IndexOf(name);
                if (index >= 0)
                {
                    Users.Remove(name);
                    Directions.Remove(Directions[index]);
                }
                   
            }
        }

    }
}
